<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>首页 - 二手平台</title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
<nav>
    <a href="/index.html">首页</a>
    <a href="#" id="logoutBtn">退出登录</a>
</nav>
<div class="container">
    <h2>欢迎来到二手交易平台！</h2>
    <div id="userInfo"></div>
    <div style="margin-top:30px;">
        <button onclick="window.location.href='/products.html'">商品浏览页</button>
        <button onclick="window.location.href='/publish.html'">商品发布页</button>
        <button onclick="window.location.href='/dashboard.html'">用户中心页</button>
        <input type="number" id="productIdInput" placeholder="输入商品ID" style="width:120px;">
        <button onclick="gotoProductDetail()">商品详情页</button>
    </div>
</div>
<script src="js/api.js"></script>
<script>
    // 获取当前用户信息
    async function loadUser() {
        const resp = await fetch('/api/users/me');
        if (resp.ok) {
            const user = await resp.json();
            if (user && user.username) {
                document.getElementById('userInfo').innerText = '当前用户：' + user.username;
            } else {
                window.location.href = '/login.html';
            }
        } else {
            window.location.href = '/login.html';
        }
    }
    loadUser();

    // 退出登录
    document.getElementById('logoutBtn').onclick = async function() {
        await fetch('/api/auth/logout', {method: 'POST'});
        window.location.href = '/login.html';
    };

    // 商品详情页跳转
    function gotoProductDetail() {
        const id = document.getElementById('productIdInput').value || 1;
        window.location.href = '/product.html?id=' + id;
    }
</script>
</body>
</html>